package com.jike.noobmoney.mvp.view.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.net.http.SslError;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.JsResult;
import android.webkit.SslErrorHandler;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.jike.noobmoney.R;
import com.jike.noobmoney.mvp.view.widget.MyWebChromeClient;
import com.jike.noobmoney.util.Logger;
import com.jike.noobmoney.util.ToastUtils;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * author: xuke.
 * date: 2017/4/28 14:26
 * 所有的前端界面都可以进这个页面,前提是前端页面没有标题栏
 */

public class MyEventActivity extends BaseActivity implements MyWebChromeClient.OpenFileChooserCallBack {

    public static final String TAG = "MyEventActivity";
    public static final String BUNDLE_KEY_URL = "MY_EVENT_BUNDLE_KEY_URL";
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.my_profile_tracker)
    ProgressBar mProgressBar;
    @BindView(R.id.common_web_browser_layout)
    WebView mWebView;


    private String mWebUrl = null;

    @Override
    protected int layoutResID() {
        return R.layout.activity_my_event;
    }

    @Override
    protected void initData() {
        Intent intent = getIntent();
        if (intent != null) {
            mWebUrl = intent.getStringExtra(MyEventActivity.BUNDLE_KEY_URL);
        }
        setWebsetting();
        mWebView.setWebChromeClient(new MyWebChromeClient(MyEventActivity.this) {

            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                if (newProgress == 100) {
                    mProgressBar.setVisibility(View.GONE);
                } else {
                    mProgressBar.setVisibility(View.VISIBLE);
                    mProgressBar.setProgress(newProgress);
                }
            }

            @Override
            public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
                return true;
            }


        });

        //加载完成后调用方法
        mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                tvTitle.setText(mWebView.getTitle());
            }

            @SuppressWarnings("deprecation")
            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                super.onReceivedError(view, errorCode, description, failingUrl);

            }

            @Override
            public void onReceivedSslError(WebView webView, SslErrorHandler sslErrorHandler, SslError sslError) {
                sslErrorHandler.proceed();
            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView webView, String url) {
                return super.shouldOverrideUrlLoading(webView, url);
            }
        });

        if (!TextUtils.isEmpty(mWebUrl)) {
            Log.e(TAG, "mWebUrl=" + mWebUrl);
            mWebView.loadUrl(mWebUrl);
        } else {
            ToastUtils.showShortToast("获取URL地址失败");
        }
    }

    @Override
    public void onBackPressed() {
        if (mWebView.canGoBack()) {
            mWebView.goBack();
        } else {
            finish();
        }
    }

    @SuppressWarnings("deprecation")
    @SuppressLint({"SetJavaScriptEnabled", "AddJavascriptInterface"})
    private void setWebsetting() {
        WebSettings webSettings = mWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        mWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
        webSettings.setDefaultTextEncodingName("UTF-8");
        webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        mWebView.addJavascriptInterface(new JsToJava(), "stub");
        webSettings.setBuiltInZoomControls(false);
        webSettings.setSupportMultipleWindows(true);
        webSettings.setUseWideViewPort(true);
        webSettings.setLoadWithOverviewMode(true);
        webSettings.setSupportZoom(false);
        webSettings.setPluginState(WebSettings.PluginState.ON);
        webSettings.setDomStorageEnabled(true);
        webSettings.setLoadsImagesAutomatically(true);
    }

    @Override
    public void openFileChooserCallBack(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {

    }

    @Override
    public boolean openFileChooserCallBackAndroid5(WebView webView, ValueCallback<Uri[]> filePathCallback, WebChromeClient.FileChooserParams fileChooserParams) {
        return false;
    }


    @OnClick({R.id.iv_back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
        }
    }

    /**
     * 和前端交互的方法
     **/
    private class JsToJava {
        @JavascriptInterface
        public void jsMethod(final String jsonStr) {

        }

        /**
         * 微信支付
         */
        @JavascriptInterface
        public void weixinPay(String jsonStr) {
            Logger.e("xuke" + jsonStr);
            ToastUtils.showShortToastSafe("微信支付");
        }
    }


}
